using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class LIST_CHARTDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procLIST_CHART_add]";
        private string _strSPUpdateName = "dbo.[procLIST_CHART_update]";
        private string _strSPDeleteName = "dbo.[procLIST_CHART_delete]";
        private string _strSPGetName = "dbo.[procLIST_CHART_get]";
        private string _strSPGetAllName = "dbo.[procLIST_CHART_getall]";
        private string _strSPGetPages = "dbo.[procLIST_CHART_getpaged]";
        private string _strSPIsExist = "dbo.[procLIST_CHART_isexist]";
        private string _strTableName = "[LIST_CHART]";
        private string _strSPGetTransferOutName = "dbo.[procLIST_CHART_gettransferout]";
        private string _strSPGetNameGadget = "dbo.[procLIST_CHART_get_byIDGADGET]";
        string _strSPGetCountName = "procLIST_CHART_getcount";
        private string _strSPDeleteName_byDelete = "dbo.[procLIST_CHART_delete_byGadget]";
        #endregion Local Variable

        #region Method
        public LIST_CHARTInfo Get(
        String DTB,
        Int32 ID,
        ref string sErr)
        {
            LIST_CHARTInfo objEntr = new LIST_CHARTInfo();
            InitConnect();
            InitSPCommand(_strSPGetName);
            AddParameter(LIST_CHARTInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_CHARTInfo.Field.ID.ToString(), ID);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}

            if (list.Rows.Count > 0)
                objEntr = (LIST_CHARTInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            LIST_CHARTInfo result = new LIST_CHARTInfo();
            result.DTB = (dt.Rows[i][LIST_CHARTInfo.Field.DTB.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_CHARTInfo.Field.DTB.ToString()]));
            result.ID = (dt.Rows[i][LIST_CHARTInfo.Field.ID.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][LIST_CHARTInfo.Field.ID.ToString()]));
            result.IDGadget = (dt.Rows[i][LIST_CHARTInfo.Field.IDGadget.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][LIST_CHARTInfo.Field.IDGadget.ToString()]));
            result.SheetChart = (dt.Rows[i][LIST_CHARTInfo.Field.SheetChart.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_CHARTInfo.Field.SheetChart.ToString()]));
            result.NameRange = (dt.Rows[i][LIST_CHARTInfo.Field.NameRange.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_CHARTInfo.Field.NameRange.ToString()]));
            result.CaptionRange = (dt.Rows[i][LIST_CHARTInfo.Field.CaptionRange.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_CHARTInfo.Field.CaptionRange.ToString()]));
            result.SubCaptionRange = (dt.Rows[i][LIST_CHARTInfo.Field.SubCaptionRange.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_CHARTInfo.Field.SubCaptionRange.ToString()]));
            result.ChartType = (dt.Rows[i][LIST_CHARTInfo.Field.ChartType.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_CHARTInfo.Field.ChartType.ToString()]));
            result.PositionReplace = (dt.Rows[i][LIST_CHARTInfo.Field.PositionReplace.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][LIST_CHARTInfo.Field.PositionReplace.ToString()]));
            result.width = (dt.Rows[i][LIST_CHARTInfo.Field.width.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][LIST_CHARTInfo.Field.width.ToString()]));
            result.height = (dt.Rows[i][LIST_CHARTInfo.Field.height.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][LIST_CHARTInfo.Field.height.ToString()]));

            return result;
        }

        public DataTable GetAll(
        String DTB,
        ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            AddParameter(LIST_CHARTInfo.Field.DTB.ToString(), DTB);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetByPos(
        String DTB,
        int pos, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            AddParameter("INX", pos);
            AddParameter(LIST_CHARTInfo.Field.DTB.ToString(), DTB);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public int GetCountRecord(
        String DTB,
        ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitSPCommand(_strSPGetCountName);
            AddParameter(LIST_CHARTInfo.Field.DTB.ToString(), DTB);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(LIST_CHARTInfo objEntr, ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitSPCommand(_strSPInsertName);
            AddParameter(LIST_CHARTInfo.Field.DTB.ToString(), objEntr.DTB);
            AddParameter(LIST_CHARTInfo.Field.ID.ToString(), objEntr.ID);
            AddParameter(LIST_CHARTInfo.Field.IDGadget.ToString(), objEntr.IDGadget);
            AddParameter(LIST_CHARTInfo.Field.SheetChart.ToString(), objEntr.SheetChart);
            AddParameter(LIST_CHARTInfo.Field.NameRange.ToString(), objEntr.NameRange);
            AddParameter(LIST_CHARTInfo.Field.CaptionRange.ToString(), objEntr.CaptionRange);
            AddParameter(LIST_CHARTInfo.Field.SubCaptionRange.ToString(), objEntr.SubCaptionRange);
            AddParameter(LIST_CHARTInfo.Field.ChartType.ToString(), objEntr.ChartType);
            AddParameter(LIST_CHARTInfo.Field.PositionReplace.ToString(), objEntr.PositionReplace);
            AddParameter(LIST_CHARTInfo.Field.width.ToString(), objEntr.width);
            AddParameter(LIST_CHARTInfo.Field.height.ToString(), objEntr.height);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(LIST_CHARTInfo objEntr)
        {
            openConnection();
            InitSPCommand(_strSPUpdateName);
            AddParameter(LIST_CHARTInfo.Field.DTB.ToString(), objEntr.DTB);
            AddParameter(LIST_CHARTInfo.Field.ID.ToString(), objEntr.ID);
            AddParameter(LIST_CHARTInfo.Field.IDGadget.ToString(), objEntr.IDGadget);
            AddParameter(LIST_CHARTInfo.Field.SheetChart.ToString(), objEntr.SheetChart);
            AddParameter(LIST_CHARTInfo.Field.NameRange.ToString(), objEntr.NameRange);
            AddParameter(LIST_CHARTInfo.Field.CaptionRange.ToString(), objEntr.CaptionRange);
            AddParameter(LIST_CHARTInfo.Field.SubCaptionRange.ToString(), objEntr.SubCaptionRange);
            AddParameter(LIST_CHARTInfo.Field.ChartType.ToString(), objEntr.ChartType);
            AddParameter(LIST_CHARTInfo.Field.PositionReplace.ToString(), objEntr.PositionReplace);
            AddParameter(LIST_CHARTInfo.Field.width.ToString(), objEntr.width);
            AddParameter(LIST_CHARTInfo.Field.height.ToString(), objEntr.height);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        String DTB,
        Int32 ID
        )
        {
            openConnection();
            InitSPCommand(_strSPDeleteName);
            AddParameter(LIST_CHARTInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_CHARTInfo.Field.ID.ToString(), ID);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        

        public Boolean IsExist(
        String DTB,
        Int32 ID
        )
        {
            InitConnect();
            InitSPCommand(_strSPIsExist);
            AddParameter(LIST_CHARTInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_CHARTInfo.Field.ID.ToString(), ID);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(LIST_CHARTInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            InitConnect();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetTransferOutName);
            AddParameter("DB", dtb);
            AddParameter("FROM", from);
            AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method
        public DataTable Get_ByIDGadget(
         String DTB,
         Int32 ID,
         ref string sErr)
        {
            LIST_CHARTInfo objEntr = new LIST_CHARTInfo();
            InitConnect();
            InitSPCommand(_strSPGetNameGadget);
            AddParameter(LIST_CHARTInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_CHARTInfo.Field.IDGadget.ToString(), ID);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}

            if (list.Rows.Count > 0)
                objEntr = (LIST_CHARTInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }

        public string Delete_byGadget(
   String DTB,
   Int32 ID
   )
        {
            openConnection();
            InitSPCommand(_strSPDeleteName_byDelete);
            AddParameter(LIST_CHARTInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_CHARTInfo.Field.IDGadget.ToString(), ID);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

    }
}
